﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PythonInvokerTest.Forms
{
    public partial class MultipleFileSelectionForm : FormBase
    {
        TextBox tb = new TextBox();
        public MultipleFileSelectionForm()
        {
            InitializeComponent();
        }

        string inputs = string.Empty;

        public string FilePaths
        {
            get
            {
                string[] paths = new string[lbPaths.Items.Count];
                for (int i = 0; i < lbPaths.Items.Count; i++)
                    paths[i] = lbPaths.Items[i].ToString();
                return string.Join(";", paths);
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    lbPaths.Items.Clear();
                    foreach (string path in value.Split(';'))
                        lbPaths.Items.Add(path);
                }
            }
        }

        private void MultipleFileSelectionForm_Load(object sender, EventArgs e)
        {
            tb.LostFocus += (s1, e1) =>
            {
                if (lbPaths.SelectedIndex >= 0)
                {
                    lbPaths.Items[lbPaths.SelectedIndex] = tb.Text;
                    tb.Hide();
                }
            };

            tb.KeyDown += (s2, e2) =>
            {
                if (e2.KeyCode == Keys.Enter)
                {
                    lbPaths.Focus();
                }
                else if (e2.KeyCode == Keys.Escape)
                {
                    bool isNew = lbPaths.SelectedItem.ToString().Length == 0;
                    lbPaths.Focus();
                    if (isNew)
                        btnDelete.PerformClick();
                }
            };

            inputs = FilePaths;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            tb.Text = "";
            lbPaths.Items.Add(tb.Text);
            lbPaths.SelectedIndex = lbPaths.Items.Count - 1;
            btnEdit.PerformClick();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (lbPaths.SelectedIndex >= 0)
            {
                tb.Width = lbPaths.Width;
                tb.Height = 16;
                lbPaths.Controls.Add(tb);
                tb.Text = lbPaths.SelectedItem.ToString();
                tb.Top = lbPaths.SelectedIndex * lbPaths.ItemHeight;
                tb.Show();
                tb.Focus();

            }
        }

        private void lbPaths_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnDelete.Enabled = lbPaths.SelectedIndex >= 0;
            btnMoveUp.Enabled = lbPaths.SelectedIndex > 0;
            btnMoveDown.Enabled = lbPaths.SelectedIndex < lbPaths.Items.Count - 1;
            // lbIDs.SelectedIndex = lbPaths.SelectedIndex;
            if (lbPaths.SelectedIndex >= 0)
            {
                btnDelete.Enabled = true;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lbPaths.SelectedIndex >= 0)
            {
                var id = lbPaths.SelectedIndex;
                lbPaths.Items.RemoveAt(lbPaths.SelectedIndex);
                if (id < lbPaths.Items.Count - 1)
                    lbPaths.SelectedIndex = id;
                else
                    lbPaths.SelectedIndex = lbPaths.Items.Count - 1;

            }
        }

        private void btnMoveUp_Click(object sender, EventArgs e)
        {
            if (lbPaths.SelectedIndex > 0)
            {
                var index = lbPaths.SelectedIndex;
                var path = lbPaths.Items[lbPaths.SelectedIndex].ToString();
                lbPaths.Items[index] = lbPaths.Items[index - 1].ToString();
                lbPaths.Items[index - 1] = path;
                lbPaths.SelectedIndex = index - 1;
            }
        }

        private void btnMoveDown_Click(object sender, EventArgs e)
        {
            if (lbPaths.SelectedIndex < lbPaths.Items.Count - 1)
            {
                var index = lbPaths.SelectedIndex;
                var path = lbPaths.Items[lbPaths.SelectedIndex].ToString();
                lbPaths.Items[index] = lbPaths.Items[index + 1].ToString();
                lbPaths.Items[index + 1] = path;
                lbPaths.SelectedIndex = index + 1;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            FilePaths = inputs;
            Close();
        }
    }
}
